package com.example.myone.Fragment;

import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;

import com.example.myone.Activity.AboutActivity;
import com.example.myone.Activity.SettingActivity;
import com.example.myone.Activity.UserArticleActivity;
import com.example.myone.Activity.WritingActivity;
import com.example.myone.Entity.ArticleCategoryEnum;
import com.example.myone.Entity.User;
import com.example.myone.Pager.MyPager;
import com.example.myone.R;
import com.example.myone.Util.ClickUtils;
import com.example.myone.Util.ImageUtils;
import com.example.myone.Util.LoginInfoUtil;

@RequiresApi(api = Build.VERSION_CODES.O)
public class MyFragment extends Fragment {
    private ImageView avatar;
    private ImageView background;
    private TextView username;

    @Override
    public void onResume() {
        super.onResume();
        User user = LoginInfoUtil.getUser(requireContext());
        // 加载图片并显示为圆形头像
        if (user != null) {
            ImageUtils.setImageByBase64Str(getContext(), avatar, user.avatar);
            ImageUtils.setImageByBase64Str(getContext(), background, user.background);
            username.setText(user.username);
        }
    }

    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (!LoginInfoUtil.isLogin(getContext())) {
            MyPager.replaceFragment(new LoginFragment());
        }
        View view = inflater.inflate(R.layout.my_pager_setting_fragment, container, false);
        ImageView logout = view.findViewById(R.id.top_exit_button);
        logout.setOnClickListener(v -> {
            // 点击按钮切换到 LoginFragment
            LoginInfoUtil.remove(requireContext());
            MyPager.replaceFragment(new LoginFragment());
        });
        avatar = view.findViewById(R.id.avatar);
        username = view.findViewById(R.id.username);
        background = view.findViewById(R.id.background_image);
        User user = LoginInfoUtil.getUser(requireContext());
        // 加载图片并显示为圆形头像
        if (user != null) {
            ImageUtils.setImageByBase64Str(getContext(), avatar, user.avatar);
            username.setText(user.username);
        }
        // 点击前往设置
        View setting = view.findViewById(R.id.my_setting_container);
        ClickUtils.setClickToOtherActivity(getContext(), SettingActivity.class, setting, avatar, background);
        // 点击前往投稿
        View writing = view.findViewById(R.id.my_writing_container);
        View write = view.findViewById(R.id.my_write);
        ClickUtils.setClickToOtherActivity(getContext(), WritingActivity.class, writing, write);
        // 点击前往关于我
        View about = view.findViewById(R.id.my_contact);
        ClickUtils.setClickToOtherActivity(getContext(), AboutActivity.class, about);
        // 点击前往全部
        View all = view.findViewById(R.id.all);
        ClickUtils.setClickToOtherActivityWithStrExtra(getContext(), UserArticleActivity.class, "title", ArticleCategoryEnum.ALL.getMessage(), all);
        // 点击前往我的点赞
        View daily = view.findViewById(R.id.my_daily);
        ClickUtils.setClickToOtherActivityWithStrExtra(getContext(), UserArticleActivity.class, "title", ArticleCategoryEnum.DAILY.getMessage(), daily);
        View reading = view.findViewById(R.id.my_reading);
        ClickUtils.setClickToOtherActivityWithStrExtra(getContext(), UserArticleActivity.class, "title", ArticleCategoryEnum.READING.getMessage(), reading);
        View question = view.findViewById(R.id.my_question);
        ClickUtils.setClickToOtherActivityWithStrExtra(getContext(), UserArticleActivity.class, "title", ArticleCategoryEnum.QUESTION.getMessage(), question);
        View longA = view.findViewById(R.id.my_long);
        ClickUtils.setClickToOtherActivityWithStrExtra(getContext(), UserArticleActivity.class, "title", ArticleCategoryEnum.LONG_ARTICLE.getMessage(), longA);
        View radio = view.findViewById(R.id.my_radio);
        ClickUtils.setClickToOtherActivityWithStrExtra(getContext(), UserArticleActivity.class, "title", ArticleCategoryEnum.RADIO.getMessage(), radio);
        return view;
    }


}
